home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Icon 8.1 / msm-1 / icont.sit / util.c < prev   
Encoding:
C/C++ Source or Header  |  1992-09-19  |  3.4 KB  |  189 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  util.c -- general utility functions.
  3.  */
  4.  
  5. #include <ctype.h>
  6. #include "::h:gsupport.h"
  7. #include "tproto.h"
  8. #include "globals.h"
  9. #include "trans.h"
  10. #include "tree.h"
  11.  
  12. #ifdef Xver
  13. xver(util.1)
  14. #endif                    /* Xver */
  15.  
  16. extern int optindex;
  17.  
  18. #ifndef Xver
  19. extern char *ofile;
  20. #endif                    /* Xver */
  21.  
  22.  
  23. /*
  24.  * Information about Icon functions.
  25.  */
  26.  
  27. /*
  28.  * Number of arguments.
  29.  */
  30.  
  31. #ifdef Xver
  32. xver(util.2)
  33. #endif                    /* Xver */
  34.  
  35. /*
  36.  * Names of Icon functions.
  37.  */
  38. char *ftable[] = {
  39. #define FncDef(p,n) Lit(p),
  40. #define FncDefV(p) Lit(p),
  41. #include "::h:fdefs.h"
  42. #undef FncDef
  43. #undef FncDefV
  44.    };
  45.  
  46. int ftbsize = sizeof(ftable)/sizeof(char *);
  47.  
  48. /*
  49.  * tcalloc - allocate and zero m*n bytes
  50.  */
  51. pointer tcalloc(m,n)
  52. unsigned int m, n;
  53.    {
  54.    pointer a;
  55.  
  56.    if (!(a = calloc(m,n)))
  57.       quit("out of memory");
  58.    return a;
  59.    }
  60.  
  61. /*
  62.  * trealloc - realloc a table making it half again larger and zero the
  63.  *   new part of the table.
  64.  */
  65. pointer trealloc(table, tblfree, size, unit_size, min_units, tbl_name)
  66. pointer table;      /* table to be realloc()ed */
  67. pointer tblfree;    /* reference to table free pointer if there is one */
  68. unsigned int *size; /* size of table */
  69. int unit_size;      /* number of bytes in a unit of the table */
  70. int min_units;      /* the minimum number of units that must be allocated. */
  71. char *tbl_name;     /* name of the table */
  72.    {
  73.    word new_size;
  74.    word num_bytes;
  75.    word free_offset;
  76.    word i;
  77.    char *new_tbl;
  78.  
  79.    new_size = (*size * 3) / 2;
  80.    if (new_size - *size < min_units)
  81.       new_size = *size + min_units;
  82.    num_bytes = new_size * unit_size;
  83.  
  84. #if IntBits == 16
  85.     {
  86.     word max_bytes = 64000;
  87.  
  88.     if (num_bytes > max_bytes) {
  89.        new_size = max_bytes / unit_size;
  90.        num_bytes = new_size * unit_size;
  91.        if (new_size - *size < min_units)
  92.           quitf("out of memory for %s", tbl_name);
  93.        }
  94.     }
  95. #endif                    /* IntBits == 16 */
  96.  
  97.    if (tblfree != NULL)
  98.       free_offset = DiffPtrs(*(char **)tblfree,  (char *)table);
  99.  
  100.    if (!(new_tbl = (char *)realloc(table, (unsigned)num_bytes)))
  101.       quitf("out of memory for %s", tbl_name);
  102.  
  103.    for (i = *size * unit_size; i < num_bytes; ++i)
  104.       new_tbl[i] = 0;
  105.  
  106.    *size = new_size;
  107.    if (tblfree != NULL)
  108.       *(char **)tblfree = (char *)(new_tbl + free_offset);
  109.  
  110.    return (pointer)new_tbl;
  111.    }
  112.  
  113. /*
  114.  * quit - immediate exit with error message
  115.  */
  116.  
  117. novalue quit(msg)
  118. char *msg;
  119.    {
  120.    quitf(msg,"");
  121.    }
  122.  
  123. /*
  124.  * quitf - immediate exit with message format and argument
  125.  */
  126. novalue quitf(msg,arg)
  127. char *msg, *arg;
  128.    {
  129.  
  130. #ifdef Xver
  131. xver(util.3)
  132. #endif                    /* Xver */
  133.  
  134. #ifndef Xver
  135.    extern char *progname;
  136.    fprintf(stderr,"%s: ",progname);
  137.    fprintf(stderr,msg,arg);
  138.    fprintf(stderr,"\n");
  139. #endif                    /* Xver */
  140.  
  141. #ifndef VarTran
  142. #ifndef Xver
  143.    if (ofile)
  144.       unlink(ofile);            /* remove bad icode file */
  145. #endif                    /* Xver */
  146. #endif                    /* VarTran */
  147.  
  148.    exit(ErrorExit);
  149.    }
  150.  
  151. /*
  152.  * tsyserr is called for fatal errors.  The message s is produced and the
  153.  *  translator exits.
  154.  */
  155. novalue tsyserr(s)
  156. char *s;
  157.    {
  158.  
  159. #ifdef Xver
  160. xver(util.4)
  161. #endif                    /* Xver */
  162.  
  163. #ifndef Xver
  164.    if (tok_loc.n_file)
  165.       fprintf(stderr, "File %s; ", tok_loc.n_file);
  166.    fprintf(stderr, "Line %d # %s\n", in_line, s);
  167. #endif                    /* Xver */
  168.  
  169.    exit(ErrorExit);
  170.    }
  171.  
  172.  
  173. /*
  174.  * round2 - round an integer up to the next power of 2.
  175.  */
  176. unsigned int round2(n)
  177. unsigned int n;
  178.    {
  179.    unsigned int b = 1;
  180.    while (b < n)
  181.       b <<= 1;
  182.    return b;
  183.    }
  184.  
  185. #ifdef Xver
  186. xver(util.5)
  187. #endif                    /* Xver */
  188.  
  189.